﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using PlugNT.Common;
using PlugNT.Custom;
using PlugNT.Cms.Core;
using PlugNT.Cms.Model;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_Systems
{
    public partial class EditSqls : AdminPage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            if (!IsPostBack)
            {
                string type = (Request.QueryString["t"] == null) ? "" : Request.QueryString["t"].Trim();
                string name = (Request.QueryString["name"] == null) ? "" : Request.QueryString["name"].Trim();
                string value = (Request.QueryString["value"] == null) ? "" : Request.QueryString["value"].Trim();

                switch (type)
                {
                    case "add":
                        Button_update.Text = "添加设置";
                        break;
                    case "edit":
                        Button_update.Text = "更新设置";
                        txtSqlKey.Text = name;
                        txtSqlValue.Text = value;
                        break;
                    case "delete":
                        Delete(name);
                        break;
                    default:
                        Response.Redirect("../Message.aspx?message=<b>[SQL绑定]</b>--失败！" + "&z=no");
                        break;
                }
                hidAction.Value = type;

            }
        }

        private void Delete(string name)
        {
            NameValue namevalue=new NameValue(name,"");
            if (WebSeting.DeleteSqlKeyValues(namevalue)=="")
            {
                Response.Redirect("SqlList.aspx");
            }
            else
            {
                Response.Redirect("../Message.aspx?message=<b>[SQL删除]</b>--失败！" + "&z=no");
            }
        }


        protected void Button_update_Click(object sender, EventArgs e)
        {
            string name =txtSqlKey.Text.Trim();
            string value =txtSqlValue.Text.Trim();
            NameValue namevalue = new NameValue(name, value);
            if (name != "" && value != "")
            {
                if (hidAction.Value == "add")
                {
                    if (WebSeting.AddSqlKeyValues(namevalue) == "")
                    {
                        Response.Redirect("SqlList.aspx");
                    }
                    else
                    {
                        Response.Redirect("../Message.aspx?message=<b>[添加SQL]</b>--失败！" + "&z=no");
                    }

                }
                else
                {
                    if (hidAction.Value == "edit")
                    {
                        if (WebSeting.UpdateSqlKeyValues(namevalue) == "")
                        {
                            Response.Redirect("SqlList.aspx");
                        }
                        else
                        {
                            Response.Redirect("../Message.aspx?message=<b>[修改SQL]</b>--失败！" + "&z=no");
                        }
                    }
                    else
                    {
                        Response.Redirect("../Message.aspx?message=<b>[SQL修改]</b>--失败，未知操作！" + "&z=no");
                    }
                }
            }
            else
            {
                Response.Redirect("../Message.aspx?message=<b>[SQL修改]</b>--失败，SQL文件为空或SQL路径为空！" + "&z=no");
            }
        }

    }
}
